const Merchant = require('../models/Merchant');
const getMerchantById = async (merchantId) => {
    try {
        const user = await Merchant.findByPk(merchantId, {
            attributes: ['id', 'name', 'password', 'email', 'phone'], // 排除密码字段
        });
        if (!user) {
            console.log('Merchant not found'); // 添加日志
            throw new Error('Merchant not found');
        }
        return user;
    } catch (err) {
        throw err;
    }
};
// 通过merchantname查询用户信息
const getMerchantByname = async (username) => {
    try {
        const merchant = await Merchant.findOne({
            where: { username: username }, // 传递查询条件
            attributes: ['id', 'username', 'password'], // 只选择需要的字段
        });
        // 如果没有找到用户
        if (!merchant) {
            console.log('Merchant not found'); // 添加日志
            throw new Error('Merchant not found');
        }

        return merchant;  // 返回找到的用户
    }
    catch (err) {
        // 捕获并重新抛出错误
        throw new Error(`Error in getMerchantByname: ${err.message}`);
    }
};


module.exports = {
    getMerchantById,
    getMerchantByname
};